#!/bin/bash

# usage: curl -s https://git.oschina.net/haide1014/init-aliyun-ecs/raw/master/disk/formatdisk-aliyun.sh| bash -

#install lvm
yum install lvm* -y

date=`date '+%Y-%m-%d_%H:%M:%S'` 

echo "A disk to init"

# list all disk
fdisk -l |grep -o "/dev/[v]d[a-z]" > /tmp/.1

# list all mount disk
df -HT |grep -o "/dev/[v]d[a-z]" > /tmp/.2   

# list all Physical Volume
pvdisplay |grep -o "/dev/[v]d[a-z]"  >> /tmp/.2

# list umount disk
grep -vwf /tmp/.2 /tmp/.1 > /tmp/.3   

echo `cat /tmp/.3`

# bak fstab
cp /etc/fstab /etc/fstab_$date

# start
for disk_no_init in `cat /tmp/.3`
	do
		echo $disk_no_init
# you can change vg_name to your Volume Group name,change lv_name to your Logical Volume name.
		vg_name=centos7_vGroup002
		lv_name=lv_data
		pvcreate $disk_no_init && vgcreate $vg_name $disk_no_init && lvcreate -l 100%FREE -n $lv_name /dev/$vg_name

		mkfs.xfs -f /dev/$vg_name/$lv_name && echo "###Logical Volume format successfully.###" || echo "###Logical Volume format failed###"
		#mkfs.ext4 /dev/$vg_name/$lv_name && echo "###Logical Volume format successfully.###" || echo "###Logical Volume format failed###"

		dir=/data ## this directory must not be used!!!	
		mkdir -p $dir   #create a  mount directory
		echo "/dev/$vg_name/$lv_name	$dir	xfs     defaults        0 0" >> /etc/fstab
		mount /dev/mapper/$vg_name/$lv_name  $dir && echo  "###Mount Logical Volume  successfully.###" || echo  "###Mount Logical Volume failed.###"
		echo `cat /etc/fstab |grep "/dev/$vg_name/$lv_name"` && echo "###Added to the startup.###" || echo "###Add to startup failed.###"
	done

#set ssh port 
sed -i "s/^#Port\ 22/Port\ 2108/g" /etc/ssh/sshd_config && service sshd restart
